import { createRouter, createWebHashHistory } from 'vue-router'
import { useUserStore } from '@/stores'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    // 登录页
    {
      path: '/login',
      component: () => import('@/views/login/LoginPage.vue')
    },
    // 首页
    {
      path: '/',
      component: () => import('@/views/layout/index.vue'),
      redirect: '/note/manage',
      children: [
        // 笔记管理
        {
          path: '/note/manage',
          component: () => import('@/views/layout/note/NoteManage.vue')
        },
        // 笔记分类
        {
          path: '/note/classification',
          component: () => import('@/views/layout/note/NoteClassification.vue')
        },

        // 用户中心
        // 用户信息
        {
          path: '/user/info',
          component: () => import('@/views/layout/user/UserInfo.vue')
        },
        // 用户头像
        {
          path: '/user/avatar',
          component: () => import('@/views/layout/user/UserAvatar.vue')
        },
        // 用户密码
        {
          path: '/user/password',
          component: () => import('@/views/layout/user/UserPassword.vue')
        }
      ]
    }
  ]
})

// 登陆访问拦截
router.beforeEach((to) => {
  // 判断是否存在token
  const userStore = useUserStore()
  if (!userStore.token && to.path !== '/login') {
    return '/login'
  }
})

export default router
